package com.jh.complicate.rpc.v1.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Administrator
 * @projectName newforward
 * @title RPCProxyServer.java
 * @package com.jh.complicate.rpc.v1.server
 * @description RPC代理服务实现
 * @date 2019/6/11 22:31
 */
public class RPCProxyServer {

    ExecutorService executorService = Executors.newCachedThreadPool();

    public void publisher(Object service, int port) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            // 不断接收请求
            while (true) {
                // BIO阻塞获取请求
                Socket socket = serverSocket.accept();

                // 提交任务到线程池处理
                executorService.submit(new ProcessorHandler(service, socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != serverSocket) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
